package TimeSqueeze.storage;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;

import TimeSqueeze.*;
import TimeSqueeze.common.*;
import TimeSqueeze.logic.*;

public class ReadFromFile {
	private String fileName = "TimeSqueezeSave.txt";
	private TaskInterface fileTask;
	
	public ReadFromFile() {
		
	}
	
	public void parseFileParameters(String taskType, String fileLine) {
		String[] fileLineParts = fileLine.split("-");
		
		switch (taskType) {
		case "timed":
			Time startTime;
			String[] parseStartTime = fileLineParts[0].split(" ");
			startTime = new Time(Integer.parseInt(parseStartTime[0]),Integer.parseInt(parseStartTime[1]),Integer.parseInt(parseStartTime[2]),Integer.parseInt(parseStartTime[3]));
			Time endTime;
			String[] parseEndTime = fileLineParts[1].split(" ");
			endTime = new Time(Integer.parseInt(parseEndTime[0]),Integer.parseInt(parseEndTime[1]),Integer.parseInt(parseEndTime[2]),Integer.parseInt(parseEndTime[3]));
			TimedTask fileLineTimedTask = new TimedTask(startTime,endTime,fileLineParts[2],fileLineParts[3]);
			fileTask = fileLineTimedTask;
			break;
			
		case "untimed":
			Task userInputTask = new Task(fileLineParts[0],fileLineParts[1]);
			fileTask = userInputTask; 
			break;
			
		case "deadline":
			Time endingTime;
			String[] parseEndingTime = fileLineParts[0].split(" ");
			endingTime = new Time(Integer.parseInt(parseEndingTime[0]),Integer.parseInt(parseEndingTime[1]),Integer.parseInt(parseEndingTime[2]),Integer.parseInt(parseEndingTime[3]));
			DeadlineTask userInputDeadlineTask = new DeadlineTask(fileLineParts[1],endingTime,fileLineParts[2]);
			fileTask = userInputDeadlineTask; 
			break;
			
		default:
			System.out.println("Invalid file input. Possible file corruption.");
			break;
		
		}
	}
	
	public void readFileToMem(ArrayList<TaskInterface> untimed,ArrayList<TaskInterface> deadline, ArrayList<TaskInterface> timed) throws IOException {
		FileReader filereader;
		boolean nextTaskType = false;
		try {
			filereader = new FileReader(fileName);
			BufferedReader fileLineReader = new BufferedReader(filereader);
			
			do {
				String fileLine = fileLineReader.readLine();
				if (fileLine.equals("!")) {
					nextTaskType = true;
				}
				else {
					parseFileParameters("untimed",fileLine);
					untimed.add(fileTask);
					nextTaskType=false;
				}
			}
			while (!nextTaskType);
			
			do {
				String fileLine = fileLineReader.readLine();
				if (fileLine.equals("!")) {
					nextTaskType = true;
				}
				else {
					parseFileParameters("deadline",fileLine);
					deadline.add(fileTask);
					nextTaskType = false;
				}
			}
			while (!nextTaskType);
			
			do {
				String fileLine = fileLineReader.readLine();
				if (fileLine.equals("!")) {
					nextTaskType = true;
				}
				else {
					parseFileParameters("timed",fileLine);
					timed.add(fileTask);
				}
			}
			while (!nextTaskType);
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			System.out.println("Error. Save file not found.");
		}
		
	}

	public String getFileName() {
		return fileName;
	}
	
	public void setFileName(String inputFileName) {
		fileName = inputFileName;
	}
}
